<template>
  <div class="home">
    <img alt="Vue logo" src="../assets/logo.png">
    <h1>测试开发系统</h1>
    <el-form :model="logindata" ref="logindata">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="logindata.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="logindata.password" type="password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "home",
  data() {
    return {
      logindata: {
        username: "",
        password: ""
      },
      crsftoken:""
    };
  },
  mounted() {
    this.crsftoken = document.getElementsByTagName("meta")["csrf-token"].content
  },
  methods: {
    submit() {
      this.axios
        .post("/login", JSON.stringify(this.logindata), {
          headers: {
            "Content-Type": "application/json",
            "X-CSRFToken": this.crsftoken
          }
        })
        .then(response => {
          if (response.data.success == 1) {
            return (window.location.href = "/home");
          }
          if (response.data.success == 0) {
            this.$message.error("用户名或密码错误！");
          }
        })
        .catch(error => {
          this.console.log(error);
        });
    }
  }
};
</script>
